
import {createRouter, createWebHashHistory } from "vue-router";

// #页面组件
import Page404 from '@/components/common/404'
import Login from '@/components/common/login'
import Home from '@/components/home'
import Register from '@/components/common/register.vue'
import ProductInfo from '@/components/product-info'
import Search from '@/components/searchInfo'
import BaseAttrs from '@/components/productInfo-baseAttr.vue'
import ProductComment from '@/components/productInfo-comment.vue'
import Login_template from '@/components/creative-tim/Login'
import request from "@/utils/request";
import Profile from "@/components/userInfo"
import ShopCar from "@/components/shopCar";
import OrderInfo from "@/components/orderInfo";
import OrderItems from "@/components/orderItems";
import cashierPage from '@/components/cashierPage'
import imCenter from "@/components/imCenter/imCenter.vue"
import imCenterChatContent from '@/components/imCenter/imCenter-chatConten'
import imCenterFriendList from '@/components/imCenter/imCenter-friendList'

//配置导出路由
const routes = [
    {path: '/:pathMatch(.*)*', name: 'NotFound', component: Page404},
    {path: '/', component: Home, meta: {title: '首页'}},
    {path: '/login', component: Login, meta: {title: '登录'}},
    {path: '/login/template', component: Login_template, meta: {title: '登录'}},
    {path: '/register', component: Register, meta: {title: '注册'}},
    {path: '/product/info', component: ProductInfo, meta: {title: '详情'},
        children: [
            {
                path: 'baseAttrs',
                component: BaseAttrs
            },
            {
                path: 'comment',
                component: ProductComment
            }
        ]
    },
    {path: '/product/search', props: true,name: 'search' ,component: Search, meta: {title: "搜索"},},
    {path: '/userinfo', name: 'userInfo' ,component: Profile, meta: {title: "个人主页"},},
    {path: '/shop-car', component: ShopCar, meta: {title: '购物车'}},
    // 订单详情页的 status 枚举（preSubmit、submitted）
    {path: '/shop-car/getOrderInfo/:skuIds/:status', name: 'orderInfo', props: true,component: OrderInfo, meta: {title: '订单详情页'}},
    {path: '/shop-car/getOrderItems/', name: 'orderItems',component: OrderItems, meta: {title: '我的订单'},},
    {path: '/cashier/:orderId/:orderSn', name: 'cashier-page', props: true,component: cashierPage, meta: {title: '收银台'}},
    {path: '/im-center', props: true, component: imCenter, meta: {title: '消息中心'},
        children: [
            {
                path: 'chat-content/:userId',
                component: imCenterChatContent,
                name: 'chatContent'
            },
            {
                path: 'friend-list/:userId',
                component: imCenterFriendList,
                name: 'friendList'
            }
        ]
    }
]

const router = createRouter({
    base: 'app',
    history: createWebHashHistory(),
    routes
})

/**
 * 路由错误回调
 */
router.onError((err, vm, info) => {
    console.log(`组件${vm.$vnode.tag}发生错误：${err.message},${info}`);
});

/**
 * 判断是否登录
 */
router.beforeEach((to, from, next) => {
    window.document.title = to.meta.title === undefined ? '默认标题' : to.meta.title
    if(to.path === '/' || to.path === '/product/search' || to.path === '/login' || to.path === '/register'){
        //放行路由
        next()
    } else {
        //询问是否登录
        request({
            url: '/auth/isLogin',
            method: 'get'
        }).then(res=>{
            if(res.code === 15009){
                //未登录
                localStorage.setItem('loginStatus', 'logout')
                router.push({
                    path: '/login'
                })
            } else if(res.code === 0){
                //已经登录
                localStorage.setItem('loginStatus', 'logging')
                next()
            } else {
                //未知返回码
                console.log('未知返回码', res.code, res.msg)
                localStorage.setItem('loginStatus', 'logout')
                router.push({
                    path: '/login'
                })
            }
        }).catch(err=>{
            console.log('检验登录状态异常',err.message)
        })
    }
})

export default router